package org.jingle.sketch.component
{
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.effects.Tween;
	
	import org.jingle.sketch.utils.Filters;

	public class Sector extends Composite
	{
		private var _fill_alpha_old:Number=0.7;
		private var _rotation:Number=0;
		private var _r:Number=0;
		private var _R:Number=10;
		private var _start:Number=0;
		private var _fold:Boolean=false;
		private var _tween:Tween;
		private var _label:TextField;
		private var _labelText:String = "";

		public function Sector(parent:Composite, origin:Point, r:Number, R:Number, angle:Number,start:Number = 0)
		{
			super(parent);
			this.origin = origin;
			_r=r;
			_R=R;
			this.angle=angle;
			_start=start;
			_label = new TextField();
			this.lineColor = 0x008EE6;
			this.fillColor = 0x007ED7;
			this.alpha = 0.7;
			this.addEventListener(MouseEvent.ROLL_OVER, rollOverEvent);
			this.addEventListener(MouseEvent.ROLL_OUT, rollOutEvent);
		}
		
		/**
		* draw sector
		@ x:
		@ y:
		@ r:inside circle radius
		@ R:outside circle radius
		@ angle: central angle 
		@ startA: initiation angle
		*/
		
		/*
		*Create by Geordi 14th Feb 2008
		*function DrawSector is drawing a sector in the flash by actionscript 3
		*rewrite by auzn
		*/
		public override function init() : void
		{
			sprite.graphics.clear();
			sprite.graphics.lineStyle(thickness, lineColor);
			sprite.graphics.beginFill(fillColor, alpha);

			if (Math.abs(angle) > 360)
			{
				angle=360;
			}
			var n:Number = Math.ceil(Math.abs(angle) / 45);
			var angleA:Number=angle / n;
			var x:int = origin.x, y:int = origin.y;
			angleA = angleA * Math.PI / 180;
			var startA:Number = _start; 
			startA = startA * Math.PI / 180;
			var startB:Number = startA;
			//起始边
			sprite.graphics.moveTo(x + r * Math.cos(startA), y + r * Math.sin(startA));
			sprite.graphics.lineTo(x + R * Math.cos(startA), y + R * Math.sin(startA));
			//外圆弧
			for (var i:uint=1; i <= n; i++)
			{
				startA += angleA;
				var angleMid1:Number=startA - angleA / 2;
				var bx:Number = x + R / Math.cos(angleA / 2) * Math.cos(angleMid1);
				var by:Number = y + R / Math.cos(angleA / 2) * Math.sin(angleMid1);
				var cx:Number = x + R * Math.cos(startA);
				var cy:Number = y + R * Math.sin(startA);
				sprite.graphics.curveTo(bx, by, cx, cy);
			}
			//内圆起点
			sprite.graphics.lineTo(x + r * Math.cos(startA),y + r * Math.sin(startA));
			//内圆弧
			for (var j:uint = n; j >= 1; j--)
			{
				startA-= angleA;
				var angleMid2:Number=startA + angleA / 2;
				var bx2:Number=x + r / Math.cos(angleA / 2) * Math.cos(angleMid2);
				var by2:Number=y + r / Math.cos(angleA / 2) * Math.sin(angleMid2);
				var cx2:Number=x + r * Math.cos(startA);
				var cy2:Number=y + r * Math.sin(startA);
				sprite.graphics.curveTo(bx2, by2, cx2, cy2);
			}
			//内圆终点
			sprite.graphics.lineTo(x + r * Math.cos(startB),y + r * Math.sin(startB));
			//完成填充
			sprite.graphics.endFill();
		}
		//rollover event
		private function rollOverEvent(event:MouseEvent):void
		{
			alpha = 1;
		}
		//rollout event
		private function rollOutEvent(event:MouseEvent):void
		{
			alpha = _fill_alpha_old;
		}
		//
		public function createTweenAfter(time:Number, value:Number, tweenTime:Number=0.6, angle:Number=0):void
		{
			var timer:Timer = new Timer(time, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerEnd);
			timer.start();
			function timerEnd(event:TimerEvent):void
			{
				createAfter(value, tweenTime, angle);
			}
		}
		//
		public function createAfter(value:Number, tweenTime:Number, angle:Number):void
		{
//			var oldStart:Number=this.start;
//			_tween=new Tween(this, "start", Strong.easeInOut, this.start, value, tweenTime, true);
//			var tmp:SectorCreator = this;
//			_tween.addEventListener(TweenEvent.MOTION_CHANGE, motioning);
//			_tween.addEventListener(TweenEvent.MOTION_FINISH, motioned);
//			function motioning(event:TweenEvent):void
//			{
//
//				if (Math.abs(tmp.angle) >= angle)
//				{
//					tmp.angle = angle;
//				}
//				else
//				{
//					tmp.angle=Math.abs(oldStart - tmp.start);
//				}
//			}
//			function motioned(event:TweenEvent):void
//			{
//				tmp.createText();
//				tmp.dispatchEvent(new FlashDynamicEvent("created"));
//			}
		}
		public function removeTweenAfter(time:Number, value:Number, tweenTime:Number=0.6, angle:Number=0):void
		{
			var timer:Timer=new Timer(time, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerEnd);
			timer.start();
			function timerEnd(event:TimerEvent):void
			{
				removeAfter(value, tweenTime, angle);
			}
		}
		public function removeAfter(value:Number, tweenTime:Number, angle:Number):void
		{
//			removeText();
//			var oldStart:Number=this.start;
//			_tween=new Tween(this, "start", Strong.easeInOut, this.start, value, tweenTime, true);
//			var tmp:SectorCreator = this;
//			_tween.addEventListener(TweenEvent.MOTION_CHANGE, motioning);
//			_tween.addEventListener(TweenEvent.MOTION_FINISH, motioned);
//
//			function motioning(event:TweenEvent):void
//			{
//				if (Math.abs(value - tmp.start) < angle)
//				{
//					tmp.angle=Math.abs(value - tmp.start);
//				}
//				else
//				{
//					tmp.angle = angle;
//				}
//			}
//			function motioned(event:TweenEvent):void
//			{
//				tmp.removeEventListener(TweenEvent.MOTION_CHANGE, motioning);
//				tmp.removeEventListener(TweenEvent.MOTION_FINISH, motioned);
//				tmp.dispatchEvent(new FlashDynamicEvent("cellRemoved"));
//			}
		}
		public function createText():void
		{
			_label = new TextField();
			_label.selectable = false;
			_label.autoSize = "center";
			_label.textColor = 0xFFFFFF;
			var fmt:TextFormat = new TextFormat();
			fmt.font = "Arial";
			_label.defaultTextFormat = fmt;
			_label.text = _labelText;
			var rect:flash.geom.Rectangle = sprite.getBounds(sprite.parent);
			_label.x = rect.x+rect.width/2-_label.width/2;
			_label.y = rect.y+rect.height/2-_label.height/2;
			sprite.addChild(_label);
		}
		public function removeText():void
		{
			if (_label)
			{
				sprite.removeChild(_label);
				_label = null;
			}
		}
		protected function update():void
		{
			init();
		}

		public function set label(value:String):void
		{
			_labelText = value;
			if (_label)
			{
				_label.text = value;
				var rect:flash.geom.Rectangle = sprite.getBounds(sprite.parent);
				_label.x = rect.x+rect.width/2 - _label.width/2;
				_label.y = rect.y+rect.height/2 - _label.height/2;
			}
		}
		
		public function get r():Number {
			return this._r;
		}
		
		public function set r(r:Number):void {
			this._r = r;
		}
		
		public function get R():Number {
			return this._R;
		}
		
		public function set R(R:Number):void {
			this._R = R;
		}
		 
	}
}